// pages/mine/profile/profile.js

let dateUtils = require('../../../utils/dateutils')
let api = require('../../../config/api')
let util = require('../../../utils/util')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        uploadPhotoName:"",
        bHasAvatar:false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getUserInfo();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    onCheckMaleClick(){
        this.setData({['detailData.gender']:1});
    },
    onCheckFemaleClick(){
        this.setData({['detailData.gender']:2});
    },
    formsubmit(e){
        this.saveInfo(e);
    },
    saveInfo(e){
        let that = this;
        wx.showLoading();
        let params = this.data.detailData;
        params.nickname=e.detail.value.nickname;
        params.avatar = this.data.uploadPhotoName;
        util.request(api.modifyUserInfo, params, 'POST').then(function (res){
            wx.hideLoading();
            if (res.code == 0){
                let pages = getCurrentPages();
                let lastPage = pages[pages.length - 2];
                lastPage.getUserInfo();

                wx.navigateBack();
            }else{
                wx.showToast({
                  title: res.message,
                  icon:"none"
                })
            }
        }).catch(function (e){
            wx.hideLoading();
            wx.showToast({
              title: e,
              icon:"none"
            })
        })
    },
    onChooseImg(){
        var that = this;
        wx.chooseMedia({
          camera: ['back'],
          mediaType:['image'],
          count:1,
          success(res){

            let avatarURL = "";
             res.tempFiles.forEach((v,i) => {
                avatarURL = v.tempFilePath;
             })

             wx.showLoading();
             wx.uploadFile({
               filePath: avatarURL,
               name: 'upfile',
               url: api.uploadFile,
               success:function (res){
                   wx.hideLoading();
                 let data = JSON.parse(res.data);
                 if (data.state == "SUCCESS"){
                     that.setData({['detailData.avatar']:data.url,uploadPhotoName:data.relaName});
                 }else{
                     console.log("SettlementApply:img upload failed, msg=" + data.msg);
                 }
               },
               fail:function(e){
                   wx.hideLoading();
                   wx.showToast({
                     title: e,
                     icon:"none"
                   })
               }
             })

             that.setData({avatarURL:avatarURL,['detailData.avatar']:avatarURL});
          }
        })
    },
    getUserInfo(){
        let that = this;

        wx.showLoading();
        util.request(api.getUserInfo, {},'POST').then(function (res){
            wx.hideLoading();
            if (res.code == 0){
                if (res.data.avatar.length > 0){
                    that.setData({bHasAvatar:true});
                }

                if (res.data.birthday == undefined || res.data.birthday.length == 0){
                    let date = new Date();
                    let currentTime =  date.getTime();
                    currentTime = dateUtils.formatLongTime(currentTime, "Y-M-D");
                    res.data.birthday = currentTime;
                }
                that.setData({detailData:res.data});
            }else{
                wx.showToast({
                    title: res.message,
                    icon:"none"
                })
            }

        }).catch(function (e){
            wx.hideLoading();
            wx.showToast({
              title: e,
              icon:"none"
            })
        })
    },
    examDateChange(e){
        let date = e.detail.value;
        this.setData({['detailData.birthday']:date});
    },
    onAgeInput(e){
        this.setData({['detailData.age']:e.detail.value});
    },
    onTrueNameInput(e){
        this.setData({['detailData.trueName']:e.detail.value});
    }


})